package flc.ast.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.youth.banner.listener.OnBannerListener;

import java.util.List;

import flc.ast.AppConfig;
import flc.ast.R;
import flc.ast.activity.LikeActivity;
import flc.ast.activity.RecommendActivity;
import flc.ast.adapter.LikeAdapter;
import flc.ast.adapter.MyBannerAdapter;
import flc.ast.adapter.RecommendTabAdapter;
import flc.ast.databinding.FragmentTutorialsBinding;
import stark.common.api.StkApi;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.base.BaseWebviewActivity;
import stark.common.basic.event.EventStatProxy;
import stark.common.bean.StkResBean;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link TutorialsFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class TutorialsFragment extends BaseNoModelFragment<FragmentTutorialsBinding> {

    private RecommendTabAdapter recommendTabAdapter;
    private LikeAdapter likeAdapter;

    @Override
    protected int onCreate() {
        return R.layout.fragment_tutorials;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        mDataBinding.tvRecommendAll.setOnClickListener(this);
        mDataBinding.tvLikeAll.setOnClickListener(this);

        mDataBinding.rvRecommendList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        recommendTabAdapter = new RecommendTabAdapter();
        mDataBinding.rvRecommendList.setAdapter(recommendTabAdapter);
        recommendTabAdapter.setOnItemClickListener(this);
        mDataBinding.rvLikeList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        likeAdapter = new LikeAdapter();
        mDataBinding.rvLikeList.setAdapter(likeAdapter);
        likeAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            case R.id.tvRecommendAll:
                startActivity(RecommendActivity.class);
                break;
            case R.id.tvLikeAll:
                startActivity(LikeActivity.class);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        StkResBean bean = (StkResBean) adapter.getItem(position);
        BaseWebviewActivity.open(mContext, bean.getUrl(), bean.getName());
    }

    @Override
    protected void initData() {
        getBannerData();
        getRecommendData();
        getLikeData();
    }

    private void getBannerData() {
        StkResApi.getTagResourceList(this, AppConfig.BANNER_DATA, StkApi.createParamMap(0, 6), false,
                new IReqRetCallback<List<StkResBean>>() {
                    @Override
                    public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                        if (success) {
                            if (result != null && result.size() > 0) {
                                MyBannerAdapter bannerAdapter = new MyBannerAdapter(result, getContext());
                                mDataBinding.banner.setAdapter(bannerAdapter, true);//false不无限循环
                                mDataBinding.banner.setBannerGalleryEffect(30, 24);//40两边露出距离，20图片之间距离
                                mDataBinding.banner.isAutoLoop(true);//false不自动轮播
                                //点击监听
                                mDataBinding.banner.setOnBannerListener(new OnBannerListener() {
                                    @Override
                                    public void OnBannerClick(Object data, int position) {
                                        StkResBean bean = bannerAdapter.getData(position);
                                        BaseWebviewActivity.open(mContext, bean.getUrl(), bean.getName());
                                    }
                                });
                            }
                        }
                    }
                });
    }

    private void getRecommendData() {
        StkResApi.getTagResourceList(null, AppConfig.RECOMMEND_DATA, StkApi.createParamMap(0, 6), false,
                new IReqRetCallback<List<StkResBean>>() {
                    @Override
                    public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                        if (success) {
                            if (result != null && result.size() > 0) {
                                recommendTabAdapter.setList(result);
                            }
                        }
                    }
                });
    }

    private void getLikeData() {
        StkResApi.getTagResourceList(null, AppConfig.LIKE_DATA, StkApi.createParamMap(0, 4), false,
                new IReqRetCallback<List<StkResBean>>() {
                    @Override
                    public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                        if (success) {
                            if (result != null && result.size() > 0) {
                                likeAdapter.setList(result);
                            }
                        }
                    }
                });
    }

}